import api from './index'
import type {
  IRoleInfo,
  IRolesResponse,
  IRoleStatsResponse,
  IPermissionCheckResponse,
  IApiResponse,
  IUserResponse,
  UserRole
} from '@/types'

// 获取所有角色
export const getAllRoles = (): Promise<IRolesResponse> => {
  return api.get('/roles')
}

// 获取角色统计
export const getRoleStats = (): Promise<IRoleStatsResponse> => {
  return api.get('/roles/stats')
}

// 根据角色获取用户列表
export const getUsersByRole = (role: UserRole): Promise<IApiResponse<{
  role: string
  displayName: string
  users: IUserResponse[]
  count: number
}>> => {
  return api.get(`/roles/${role}/users`)
}

// 检查用户权限
export const checkUserPermission = (userId: string | number, permission: string): Promise<IPermissionCheckResponse> => {
  return api.get(`/roles/users/${userId}/permission`, { params: { permission } })
}

// 比较角色权限
export const compareRoles = (role1: UserRole, role2: UserRole): Promise<IApiResponse<{
  role1: IRoleInfo
  role2: IRoleInfo
  comparison: number
  result: string
}>> => {
  return api.get('/roles/compare', { params: { role1, role2 } })
}
